package com.atguigu.cookies;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName: CookieServlet01
 * @Description:
 * @Author: wty
 * @Date: 2022/12/14
 */
@WebServlet("/cookie01")
public class CookieServlet01 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        // 1.创建一个cookie对象
        Cookie cookie = new Cookie("uname", "jim");

        // 可以给cookie设置有效时长1800秒,30分钟
        cookie.setMaxAge(1800);

        // 可以给cookie设置路径，当前路径才会带cookie过来
        cookie.setDomain("Path");

        // 2.将cookie保存到客户端
        response.addCookie(cookie);

        request.getRequestDispatcher("hello.html").forward(request, response);
    }
}
